﻿/*!	@file	r_math_colorRGBf.cpp
	@brief	浮動少数値色定義
Copyright (c) 2009 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "r_math_colorRGBf.h"
#include "r_math_colorRGBi.h"
#include "r_math_colorHSLf.h"
#include <algorithm>

namespace r{
	namespace math{
		ColorRGBf :: ColorRGBf ( void )
		{
			r = g = b = a = 0;
		}

		ColorRGBf :: ColorRGBf ( const ColorRGBi &color )
		{
			r = static_cast<r_f32> ( color.r ) / 255.0f;
			g = static_cast<r_f32> ( color.g ) / 255.0f;
			b = static_cast<r_f32> ( color.b ) / 255.0f;
			a = static_cast<r_f32> ( color.a ) / 255.0f;
		}
		ColorRGBf :: ColorRGBf ( r_f32 _r, r_f32 _g, r_f32 _b, r_f32 _a )
		{
			r = _r;
			g = _g;
			b = _b;
			a = _a;
		}
		ColorRGBf :: ColorRGBf ( r_f32 *_rgba )
		{
			r = _rgba[0];
			g = _rgba[1];
			b = _rgba[2];
			a = _rgba[3];
		}

		ColorRGBf :: operator ColorRGBi() const
		{
			r_f32 _r[3] = {0, r, 1.0f};
			r_f32 _g[3] = {0, g, 1.0f};
			r_f32 _b[3] = {0, b, 1.0f};
			r_f32 _a[3] = {0, a, 1.0f};
			return ColorRGBi (
					   static_cast<r_ui8> ( _r[ ( r > 0 ) + ( r > 1.0f ) ] * 255 ),
					   static_cast<r_ui8> ( _g[ ( g > 0 ) + ( g > 1.0f ) ] * 255 ),
					   static_cast<r_ui8> ( _b[ ( b > 0 ) + ( b > 1.0f ) ] * 255 ),
					   static_cast<r_ui8> ( _a[ ( a > 0 ) + ( a > 1.0f ) ] * 255 ) );
		}
		ColorRGBf :: operator ColorHSLf() const
		{
			r_f32 maxc = std::max ( std::max ( r, g ), b );
			r_f32 minc = std::min ( std::min ( r, g ), b );

			r_f32 l = ( maxc + minc ) * 0.5f;
			r_f32 s, h;
			r_f32 diff = maxc - minc;

			if ( diff < 0.000001f ) {
				s = h = 0;
			} else {
				if ( l <= 0.5f ) {
					s = diff / ( maxc + minc );
				} else {
					s = diff / ( 2 - maxc - minc );
				}
				r_f32 cr = ( maxc - r ) / diff;
				r_f32 cg = ( maxc - g ) / diff;
				r_f32 cb = ( maxc - b ) / diff;
				if ( maxc == r ) {
					h = cb - cg;
				} else if ( maxc == g ) {
					h = 2.0f + cr - cb;
				} else {
					h = 4.0f + cg - cr;
				}
				h /= 6.0f;
				if ( h < 0 ) {
					h += 1.0f;
				}
			}
			return ColorHSLf ( h, s, l, a );
		}

		r_bool	ColorRGBf :: operator == ( const ColorRGBf &src ) const
		{
			return ( r == src.r ) & ( g == src.g ) & ( b == src.b ) & ( a == src.a );
		}
		r_bool	ColorRGBf :: operator != ( const ColorRGBf &src ) const
		{
			return ( r != src.r ) | ( g != src.g ) | ( b != src.b ) | ( a != src.a );
		}
		ColorRGBf ColorRGBf :: operator + ( const ColorRGBf &src ) const
		{
			return ColorRGBf ( r + src.r, g + src.g, b + src.b, a + src.a );
		}
		const ColorRGBf&	ColorRGBf :: operator += ( const ColorRGBf &src )
		{
			r += src.r;
			g += src.g;
			b += src.b;
			a += src.a;
			return *this;
		}
		ColorRGBf	ColorRGBf :: operator * ( const ColorRGBf &src ) const
		{
			return ColorRGBf ( r * src.r, g * src.g, b * src.b, a * src.a );
		}
		const ColorRGBf&	ColorRGBf :: operator *= ( const ColorRGBf &src )
		{
			r *= src.r;
			g *= src.g;
			b *= src.b;
			a *= src.a;
			return *this;
		}
		ColorRGBf ColorRGBf :: operator + ( r_f32 src ) const
		{
			return ColorRGBf ( r + src, g + src, b + src, a + src );
		}
		const ColorRGBf&	ColorRGBf :: operator += ( r_f32 src )
		{
			r += src;
			g += src;
			b += src;
			a += src;
			return *this;
		}
		ColorRGBf ColorRGBf :: operator * ( r_f32 src ) const
		{
			return ColorRGBf ( r * src, g * src, b * src, a * src );
		}
		const ColorRGBf&	ColorRGBf :: operator *= ( r_f32 src )
		{
			r *= src;
			g *= src;
			b *= src;
			a *= src;
			return *this;
		}
	}
}


